package com.lc.user.express.ordering;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.ManageUsedRouteAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetMyAddr;
import com.lc.user.express.model.UsedRouteModel;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/7. 管理常用路线
 */
public class ManageUsedRouteActivity extends BaseSecondActivity {
    public static OnRefreshView onRefreshView = null;
    private ManageUsedRouteAdapter adapter = null;
    private List<UsedRouteModel> list = new ArrayList<>();
    private LinearLayout ll_empty;//空白显示
    private RelativeLayout rl_add;//添加按钮

    private ListView lv;
    private GetMyAddr getMyAddr = new GetMyAddr(AppContext.myPreferences.getUid(), new AsyCallBack<GetMyAddr.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetMyAddr.Info info) throws Exception {
            list.clear();
            list.addAll(info.dataList);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);
            ll_empty.setVisibility(View.VISIBLE);
            lv.setEmptyView(ll_empty);
            adapter.updateListView(list);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_used_route);
        setTitle("管理常用路线");
        initView();
        getRoute();

    }

    private void initView() {
        rl_add = (RelativeLayout) findViewById(R.id.rl_add);
        rl_add.setOnClickListener(this);
        ll_empty = (LinearLayout) findViewById(R.id.ll_empty);
        lv = (ListView) findViewById(R.id.lv);
        adapter = new ManageUsedRouteAdapter(cnt, list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("routename", list.get(position).getName());
                bundle.putString("routeId", list.get(position).getRouteId());
                show(AddUsedRouteActivity.class, AddUsedRouteActivity.TYPE_EDIT, bundle);
            }
        });
        onRefreshView = new OnRefreshView() {
            @Override
            public void onRefresh() {
                getRoute();
            }
        };

    }

    private void getRoute() {
        getMyAddr.execute(cnt);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rl_add:
                show(AddUsedRouteActivity.class, AddUsedRouteActivity.TYPE_ADD);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onRefreshView = null;
    }

    public interface OnRefreshView {
        void onRefresh();
    }
}
